<?php
class TagLogic {
	private static $table = "recipe_tag";
	const RECIPE_TAG = 0;
	const FOOD_TAG = 1;
	
	/**
	 * 获取标签应该具有的字体大小 用于标签云
	 * @param unknown_type $num
	 */
	static function getTagFontSize($num) {
		$comps = array(0, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136);
		for ($i = 0; $i < 16; $i ++) {
			if ($num <= $comps[$i]) return $i + 11;
		}
		return 27;
	}
	
	/**
	 * 获取格式化的标签字符串。返回格式为[tag,]
	 *
	 * @param unknown_type $tags
	 */
	static function getFomatTags($tags) {
		return implode(",", self::getTagList($tags));
	}
	
	/**
	 * 获取标签列表
	 *
	 * @param unknown_type $tags
	 * @return unknown
	 */
	static function getTagList($tags) {
		if (!$tags) return array();
		$tagList = preg_split("/[,;\s]+/", $tags); //加入中文标点时会出现分析错误
		$tagList = array_unique($tagList);
		$tagList = array_diff($tagList, array(""));
		return $tagList;
	}
	
	/**
	 * 获取用户的标签
	 *
	 * @param unknown_type $num
	 * @param unknown_type $actorId
	 */
	static function getActorHotTags($num=20, $actorId) {
		$sql = "select tag,count(*) num from ".self::$table." where actor_id=".$actorId." group by tag order by num desc limit ".$num;
		$db = DBMgr::getSlaver(self::$table);
		$rs = $db->query($sql);
		$ret = array();
		while($rs->hasNext()) {
			$row = $rs->next();
			$ret[] = array(
				"name" => $row["tag"],
				"num" => $row["num"],
			);
		}
		return $ret;
	}
	
	/**
	 * 获取用户的标签云
	 *
	 * @param unknown_type $num 如果为0表示所有标签
	 * @param unknown_type $actorId
	 */
	static function getActorTagClouds($num=0, $actorId) {
		$sql = "select tag,count(*) num from ".self::$table." where actor_id=".$actorId.
			" group by tag order by null ".($num > 0 ? " limit ".$num : "");
		$db = DBMgr::getSlaver(self::$table);
		$rs = $db->query($sql);
		$ret = array();
		while($rs->hasNext()) {
			$row = $rs->next();
			$ret[] = array(
				"name" => $row["tag"],
				"num" => $row["num"],
			);
		}
		return $ret;
	}

	/**
	 * 获取指定菜谱下的标签
	 *
	 * @param unknown_type $num
	 * @param unknown_type $recipeId 未指定则表示所有的菜谱
	 * @return unknown
	 */
	static function getHotTags($num=20, $recipeId = 0) {
		$whereCause = "";
		if ($recipeId > 0) {
			$whereCause = " where recipe_id=".$recipeId;
		}
		$sql = "select tag,count(*) num from ".self::$table.$whereCause." group by tag order by num desc limit ".$num;
		$db = DBMgr::getSlaver(self::$table);
		$rs = $db->query($sql);
		$ret = array();
		while($rs->hasNext()) {
			$row = $rs->next();
			$ret[] = array(
				"name" => $row["tag"],
				"num" => $row["num"],
			);
		}
		return $ret;
	}
	/**
	 * 获取指定标签下的菜谱
	 *
	 * @param unknown_type $tag
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 * @param unknown_type $actorId
	 * @return PageResult
	 */
	static function pagedGetRecipeIds($tag, $page, $pageSize, $actorId=0) {
		$pageSize = 20;
		$whereCause = $actorId > 0 ? " and actor_id=".$actorId : "";
		$sql = "select recipe_id from ".self::$table." where tag=? ".$whereCause." order by recipe_id desc";
		$db = DBMgr::getSlaver(self::$table);
		return $db->fullPagedQuery($sql, $page, $pageSize, array($tag));
	}
	/**
	 * 分页获取标签
	 *
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 * @return PageResult
	 */
	static function pagedGetTags($page, $pageSize) {
		$sql = "select count(distinct(tag)) from ".self::$table;
		$db = DBMgr::getSlaver(self::$table);
		$total = intval($db->querySingle($sql)); 
		if ($total == 0) return PageResult::build(0,$page, $pageSize, null);
		$sql = "select tag name,count(*) num from ".self::$table." group by tag order by num desc";
		return $db->fullPagedQuery($sql, $page, $pageSize, array(), $total);
	}

	/**
	 * 增加标签
	 *
	 * @param unknown_type $tagList
	 * @param unknown_type $recipeId
	 * @param unknown_type $actorId
	 * @param unknown_type $typeId
	 */
	static function addTags($tagList, $recipeId, $actorId, $typeId) {
		if (!is_array($tagList)) {
			$tagList = self::getTagList($tags);
		}
		if (count($tagList) == 0) return;
		
		$db = DBMgr::getMaster(self::$table);
		foreach($tagList as $tag) {
			$sql = "insert ignore into ".self::$table."(tag,recipe_id,actor_id,type_id) values(?,?,?,?)";
			$db->execute($sql,array($tag, $recipeId, $actorId, $typeId));
		}
	}
	
	/**
	 * 更新标签
	 *
	 * @param unknown_type $oldTagList
	 * @param unknown_type $tagList
	 * @param unknown_type $recipeId
	 * @param unknown_type $actorId
	 * @param unknown_type $typeId
	 */
	static function updateTags($oldTagList, $tagList, $recipeId, $actorId, $typeId) {
		if (!is_array($oldTagList)) {
			$oldTagList = self::getTagList($oldTagList);
		}
		if (!is_array($tagList)) {
			$tagList = self::getTagList($tags);
		}
		// 不用做任何操作的标签
		$sameList = array_intersect($oldTagList, $tagList);
		// 求出要删除的标签
		$deleteList = array_diff($oldTagList, $sameList);
		// 求出要增加的标签
		$addList = array_diff($tagList, $sameList);
		// 如果没有要删除或者增加的标签，则直接退出
		if (!$deleteList && !$addList) return;
				
		$db = DBMgr::getMaster(self::$table);
		// 删除标签
		foreach($deleteList as $tag) {
			$sql = "delete from ".self::$table." where tag=? and recipe_id=? and actor_id=? and type_id=?";
			$db->execute($sql, array($tag, $recipeId, $actorId, $typeId));
		}
		
		// 增加标签
		foreach($addList as $tag) {
			$sql = "insert ignore into ".self::$table."(tag,recipe_id,actor_id,type_id) values(?,?,?,?)";
			$db->execute($sql,array($tag, $recipeId, $actorId, $typeId));
		}
	}
	
	/**
	 * 删除标签
	 *
	 * @param unknown_type $recipeId
	 * @param unknown_type $actorId
	 * @param unknown_type $typeId
	 */
	static function deleteTags($recipeId, $actorId, $typeId) {
		$sql = "delete from ".self::$table." where recipe_id=? and actor_id=? and type_id=?";
		$db = DBMgr::getMaster(self::$table);
		$db->execute($sql, array($recipeId, $actorId, $typeId));
	}
}
?>